
var xmlHttpRequest,
    allItemNumber       = [],
    allItemId        = [];
    // allItemTotalPrice   = [],
var items               = $('input.item'),
    itemsPrice          = $('.listPrice'),
    itemsTotal          = $('.total');

function getAllCartItem() {

    for (var i = 0, length = items.length; i < length; i++) {
        var itemName = items[i].getAttribute('name');
        itemPrice   = itemsPrice[i].getAttribute('name');
        allItemId.push(itemName);
        // allItemTotalPrice.push($('label[name='+itemTotal+']').text().substring(1));
        allItemNumber.push($('input[name=' + itemName + ']').val());
        // allItemName.push($("#" + '${itemsPriceId}').val());
    }
}
$(function(){
    var w=1;
    console.log('调用'+w+'次');
    w++;
    var itemName,
        itemPrice,
        itemTotal;
    //获取全部价格和总价
    //绑定onblur事件
    for(var i=0;i<items.length;i++){


            itemName    = items[i].getAttribute('name');
            $('input[name='+itemName+']').on('blur',function (){
                getAllCartItem();
                console.log(allItemNumber);
                console.log(allItemId);
                $.ajax({
                   type     : 'POST',
                    url     : 'http://localhost:8080/jpet_store_war_exploded/ChangeMyCart',
                    //将itemID和quantity通过json数组传给后端
                    data    : {
                       itemsQuantity    :JSON.stringify(allItemNumber),
                       itemsId          :JSON.stringify(allItemId),
                       // itemsTotalPrice  :allItemTotalPrice
                    },
                    dataType: 'json',
                    success : function (data){
                       //处理数据
                        var totalList = eval(data);
                        console.log(totalList);
                        setTotal(totalList);
                        // 清空number
                        allItemNumber   = [];
                        allItemId       = [];
                    },
                    error   : function (errorMsg){
                        console.log(errorMsg);
                    }
                });
            })
        }


});

function setTotal(totalList){
    var i,
        length = totalList.length-1;
    for (i = 0; i < length; i++){
        var itemName = itemsTotal[i].getAttribute('name');
        if(totalList[i]<1){
            $('label[name='+itemName+']')();
        }else{

            $('label[name='+itemName+']').text('$'+totalList[i].toFixed(2));
        }

    }
    $('#subtotal').text('$'+totalList[length].toFixed(2));
}
function updateCart(thisItemId) {
    var itemId=thisItemId;
    console.log('itemId='+itemId);
    // var quantity = document.getElementById("quantity").value;
    // sendRequest("ChangeMyCartServlet?quantity="+ quantity);
    sendRequest("ChangeMyCartServlet?itemId="+ itemId);
}

function sendRequest(url) {
    xmlHttpRequest.open("GET", url, true);
    xmlHttpRequest.onreadystatechange = processResponse;
    xmlHttpRequest.send(null);
}

function processResponse() {
    if (xmlHttpRequest.readyState === 4) {
        if (xmlHttpRequest.status === 200) {
            var resp = xmlHttpRequest.responseText;
            var array = resp.split(",");
            var quantity = document.getElementById("quantity");
            var total = document.getElementById("total");
            var subtotal = document.getElementById("subtotal");
            console.log(item);
            quantity.innerText = array[0];
            total.innerText = array[1];
            subtotal.innerText = array[2];
        }
    }
}

